package com.xidian.zhiancloud.service.lz.hiddeninvestigation.hiddeninvestigationbasic;

import com.xidian.zhiancloud.dao.HiddenDangerTypeBasicsMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.HiddenDangerTypeBasics;
import com.xidian.zhiancloud.model.HiddenDangerTypeBasicsExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
public class HiddenInvestigationTypeService {

    @Autowired
    private HiddenDangerTypeBasicsMapper hiddenDangerTypeBasicsMapper;

    public List<HiddenDangerTypeBasics> getList(String type, int current, int size) {
        HiddenDangerTypeBasicsExample example = new HiddenDangerTypeBasicsExample();
        HiddenDangerTypeBasicsExample.Criteria criteria = example.createCriteria();
        if (type!=null || "".equals(type)) {
            criteria.andTypeLike('%' + type + '%');
        }
        return hiddenDangerTypeBasicsMapper.selectByExampleWithRowbounds(example, new RowBounds(current, size));

    }

    public void insert(HiddenDangerTypeBasics hiddenDangerTypeBasics) {
        int count = hiddenDangerTypeBasicsMapper.insert(hiddenDangerTypeBasics);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void update(HiddenDangerTypeBasics hiddenDangerTypeBasics) {
        HiddenDangerTypeBasicsExample example = new HiddenDangerTypeBasicsExample();
        HiddenDangerTypeBasicsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(hiddenDangerTypeBasics.getId());
        HiddenDangerTypeBasics hiddenDangerType = new HiddenDangerTypeBasics();
        hiddenDangerType.setCompanyId(hiddenDangerTypeBasics.getCompanyId());
        hiddenDangerType.setInvestigationCycle(hiddenDangerTypeBasics.getInvestigationCycle());
        hiddenDangerType.setType(hiddenDangerTypeBasics.getType());
        hiddenDangerType.setOrganizationLevel(hiddenDangerType.getOrganizationLevel());
        int count = hiddenDangerTypeBasicsMapper.updateByExampleSelective(hiddenDangerType, example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void delete(HiddenDangerTypeBasics hiddenDangerTypeBasics) {
        HiddenDangerTypeBasicsExample example = new HiddenDangerTypeBasicsExample();
        HiddenDangerTypeBasicsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(hiddenDangerTypeBasics.getId());

        int count = hiddenDangerTypeBasicsMapper.deleteByExample(example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            int result = hiddenDangerTypeBasicsMapper.deleteByPrimaryKey(id);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }
}
